<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Orders_post{
    public function __construct(){
        $this->load->library('curl');
    }
    
    public function demo2(){
        //根据trade表单获取到account1的ID和account2的ID

        //分别获取account1和account2的session并存入account中

        //insert 到trade表里去
        for($i=0;$i<5;$i++){
            echo "<b>执行{$i}次</b><hr />";
            //根据session调用交易方法
            //$this->demo($session1,$session2);
        }
    }

    public function order_post($market,$account1_id,$account2_id,$headers1,$headers2){	
        echo 	$market;
        echo 	$account1_id;
        echo 	$account2_id;
        echo 	$headers1;
        echo 	$headers2;
        die();
		//get order_book
		$url="https://t.sccx.exchange/api/v2/order_book?market=$market&bids_limit=1&asks_limit=1";
		$order_book=$this->curl->ssl()->http_header($headers2)->simple_get($url);
		echo $order_book."<hr />";
		$order_book=json_decode($order_book,false);
		//get the middle price
		$buy=$order_book->bids[0]->price;
		$sell=$order_book->asks[0]->price;
		$price_rand=mt_rand(1,9)/10;

		$my_price=$buy+($sell-$buy)*$price_rand;
		echo "买入价$buy,卖出价:$sell,本轮定价:$my_price<hr />";
	/*
		如果没有订单得话 这个价格怎么定	
		if($my_price==0){
			$my_price=8872.5;
		}*/
		//数量不要超过1
		$volume=mt_rand(1,9)/10;
		//create account1 sell order
		$url="https://t.sccx.exchange/api/v2/orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"sell",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_sell_order=$this->curl->ssl()->http_header($headers1)->simple_post($url, $post_data);
        echo "account1卖出订单".$create_sell_order."<hr />";
        $this->insert_orders($create_sell_order,$headers1,"account1");		                                     

		//create account2 buy order
		$url="https://t.sccx.exchange/api/v2/orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"buy",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_buy_order=$this->curl->ssl()->http_header($headers2)->simple_post($url, $post_data);
		echo "account2买入订单".$create_buy_order."<hr />";
        $this->insert_orders($create_buy_order,$headers2,"account2");		                                     

		//create account2 sell order
		$url="https://t.sccx.exchange/api/v2/orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"sell",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_sell_order=$this->curl->ssl()->http_header($headers2)->simple_post($url, $post_data);
        echo "account2卖出订单".$create_sell_order."<hr />";
        $this->insert_orders($create_sell_order,$headers2,"account2");		                                     

		//create account1 buy order
		$url="https://t.sccx.exchange/api/v2/orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"buy",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_buy_order=$this->curl->ssl()->http_header($headers1)->simple_post($url, $post_data);
        echo "account1买入订单".$create_buy_order."<hr />";
        $this->insert_orders($create_buy_order,$headers1,"account1");		                                     

    }
    //,$headers,$account
    public function insert_orders($create_order,$headers,$account){
        //存入数据库 表orders
        $this->load->model('orders_model','orders');
        $orders['order_id']=json_decode($create_order)->id;
        $orders['account']=$account;
        $orders['side']=json_decode($create_order)->side;
        $orders['ord_type']=json_decode($create_order)->ord_type;
        $orders['price']=json_decode($create_order)->price;
        $orders['avg_price']=json_decode($create_order)->avg_price;
        $orders['state']=json_decode($create_order)->state;
        $orders['market']=json_decode($create_order)->market;
        $orders['created_at']=json_decode($create_order)->created_at;
        $orders['volume']=json_decode($create_order)->volume;
        $orders['remaining_volume']=json_decode($create_order)->remaining_volume;
        $orders['executed_volume']=json_decode($create_order)->executed_volume;
        $orders['trades_count']=json_decode($create_order)->trades_count;
        $i=$this->orders->insert($orders);

        //update order trades
        $url="https://t.sccx.exchange/api/v2/order?id=".json_decode($create_order)->id;
		$order_trades=$this->curl->ssl()->http_header($headers)->simple_get($url);
        $orders['remarks']=json_encode(json_decode($order_trades)->trades);   
        $i=$this->orders->update($orders, array('id='.$i)); 
    }
    
}